//
//  MYSelectChannelView.m
//  MiaoYin
//
//  Created by juweitu on 2020/7/8.
//  Copyright © 2020 thy. All rights reserved.
//

#import "MYSelectChannelView.h"
#import "MYSelectChannelCollectionViewCell.h"
#import "MYChannelModel.h"

@interface MYSelectChannelView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UIView *my_mainView;
    UICollectionView* m_collectionView;
}
@property (nonatomic,strong) NSArray *my_itemArray;
@property (nonatomic,copy) MYSelectChannelCompletion completion;
@property (nonatomic,assign) CGFloat my_height;
@property (nonatomic,strong) NSIndexPath *selectIndexPath;
@end

@implementation MYSelectChannelView

- (void)showWithArray:(NSArray *)array
           completion:(MYSelectChannelCompletion)completion
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    
    self.my_height = (array.count / 4) * 50 + (array.count % 4) * 50 + 20;
    
    [self layout];
    
    self.completion = completion;
    self.my_itemArray = array;
    [m_collectionView reloadData];
    
    [UIView animateWithDuration:0.3 animations:^{
        self->my_mainView.transform = CGAffineTransformTranslate(self->my_mainView.transform, 0, -self->my_mainView.bounds.size.height);
    } completion:^(BOOL finished) {
    }];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    if(self = [super initWithFrame:frame])
    {
        self.my_height = 0;
//        [self layout];
    }
    
    return self;
}

- (void)layout
{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    
    my_mainView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-self.my_height-50, SCREEN_WIDTH, self.my_height+50)];
    my_mainView.backgroundColor = [UIColor whiteColor];
    my_mainView.clipsToBounds = YES;
    my_mainView.alpha = 1.0;
    [self addSubview:my_mainView];
    
    UILabel *hintLabel = [[UILabel alloc] init];
    hintLabel.textColor = MY_Text_Color_Theme1;
    hintLabel.font = [UIFont boldSystemFontOfSize:14];
    hintLabel.text = @"选择频道";
    [my_mainView addSubview:hintLabel];
    
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeButton setImage:[UIImage imageNamed:@"ic_shop_close"] forState:0];
    [closeButton addTarget:self action:@selector(closeButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [my_mainView addSubview:closeButton];
    
    UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.minimumLineSpacing = 20;
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-55)/4, 30);
    
    m_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    [m_collectionView registerClass:[MYSelectChannelCollectionViewCell class] forCellWithReuseIdentifier:@"MYSelectChannelCollectionViewCell"];
    m_collectionView.backgroundColor = [UIColor whiteColor];
    m_collectionView.showsHorizontalScrollIndicator = NO;
    m_collectionView.showsVerticalScrollIndicator = NO;
    m_collectionView.dataSource = self;
    m_collectionView.delegate = self;
    [my_mainView addSubview:m_collectionView];
    
    {
        [hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(MY_Padding));
            make.top.equalTo(@16);
        }];
        
        [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(hintLabel);
            make.height.width.equalTo(@36);
            make.right.equalTo(@(-MY_Padding));
        }];
        
        [m_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self->my_mainView);
            make.top.equalTo(hintLabel.mas_bottom).offset(20);
        }];
    }
    
    UIBezierPath* maskPath = [UIBezierPath bezierPathWithRoundedRect:my_mainView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
    CAShapeLayer* maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = my_mainView.bounds;
    maskLayer.path = maskPath.CGPath;
    my_mainView.layer.mask = maskLayer;
    
    my_mainView.transform = CGAffineTransformTranslate(my_mainView.transform, 0, my_mainView.bounds.size.height);
}

#pragma mark - UICollectionView delgate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.my_itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYSelectChannelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MYSelectChannelCollectionViewCell" forIndexPath:indexPath];
    
    MYChannelModel *model = self.my_itemArray[indexPath.row];
    cell.label.text = model.typeName;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath != self.selectIndexPath)
    {
        MYChannelModel *model = self.my_itemArray[indexPath.row];
        
        MYSelectChannelCollectionViewCell *cell = (MYSelectChannelCollectionViewCell *)[collectionView cellForItemAtIndexPath:self.selectIndexPath];
        cell.label.textColor = MY_Text_Color_Theme2;
        cell.label.backgroundColor = MY_VC_Back_Gray_Color;
        cell = (MYSelectChannelCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.label.textColor = MY_ColourMatchingSpecifications_Theme1;
        cell.label.backgroundColor = [UIColor colorWithUInt:0xEAF4FF];
        self.selectIndexPath = indexPath;
        
        if (self.completion)
        {
            self.completion(model);
        }
    }
}

- (void)closeButtonTouchUpInside:(UIButton *)sender
{
    [self dismiss];
}

- (void)dismiss
{
       [UIView animateWithDuration:0.20 animations:^{
           self->my_mainView.transform = CGAffineTransformTranslate(self->my_mainView.transform, 0, self->my_mainView.bounds.size.height);
       } completion:^(BOOL finished) {
           [UIView animateWithDuration:0.20 animations:^{
               self.alpha = 0.0;
           } completion:^(BOOL finished) {
               self.hidden = YES;
               [self removeFromSuperview];
           }];
       }];
}

@end
